package com.lhn.controller;

import com.lhn.entity.Menu;
import com.lhn.entity.Order;
import com.lhn.entity.ResponseMsg;
import com.lhn.entity.User;
import com.lhn.feign.OrderFeign;
import feign.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author lhn
 * @date 2024-10-14 22:25
 */

@Controller
@RequestMapping("consumer/order/")
public class OrderController {

    @Autowired
    private OrderFeign orderFeign;

    @GetMapping("save/{mid}")
    public String save(
            @PathVariable("mid") long mid,
            HttpSession httpSession
            ) {
        Order order = new Order();
        User user = (User) httpSession.getAttribute("user");
        Menu menu = new Menu();
        menu.setId(mid);
        order.setUser(user);
        order.setMenu(menu);
        orderFeign.save(order);
        return "index";
    }

    @GetMapping("findAll/{index}/{limit}")
    @ResponseBody
    public ResponseMsg<List<Order>> findAll(@PathVariable("index") int index, @PathVariable("limit") int limit) {
        return new ResponseMsg<>(
                "0",
                "",
                orderFeign.count(),
                orderFeign.findAll(index, limit)
        );
    }

    @GetMapping("count")
    @ResponseBody
    public int count() {
        return orderFeign.count();
    }

    @GetMapping("countByUid/{uid}")
    @ResponseBody
    public int countByUid(@PathVariable("uid") int uid) {
        return orderFeign.countByUid(uid);
    }

    @GetMapping("findAllByUid")
    @ResponseBody
    public ResponseMsg<List<Order>> findAllByUid(
            @RequestParam("page") int page,
            @RequestParam("limit") int limit,
            HttpSession httpSession)
             {
        int index = (page - 1) * limit;
        long uid = ((User) httpSession.getAttribute("user")).getId();
        return new ResponseMsg<>(
                "0",
                "",
                orderFeign.countByUid(uid),
                orderFeign.findAllByUid(uid, index, limit)
        );
    }

    @GetMapping("findAllByState/{state}")
    @ResponseBody
    public ResponseMsg<List<Order>> findAllByState(
            @PathVariable("state") int state,
            @Param("page") int page,
            @Param("limit") int limit
    ) {
        int index = (page - 1) * limit;
        return new ResponseMsg<>(
                "0",
                "",
                orderFeign.countByState(state),
                orderFeign.findAllByState(state, index, limit)
        );
    }

    @GetMapping("updateState/{id}/{state}")
//    @ResponseBody
    public String updateState(@PathVariable("id") long id, @PathVariable("state") int state) {
        orderFeign.updateState(id, state);
        return "redirect:/consumer/redirect/order_handler";
    }

    @GetMapping("findAllState")
    @ResponseBody
    public ResponseMsg<List<Order>> findAllState(
            @Param("page") int page,
            @Param("limit") int limit
    ) {
        int index = (page - 1) * limit;
        return new ResponseMsg<>(
                "0",
                "",
                orderFeign.count(),
                orderFeign.findAllState(index, limit)
        );
    }

}
